package com.anolesoft.amljmes.perm.service.userMenu.mapper;

//import com.anolesoft.common.entity.UiResourceDO;
import com.anolesoft.amljmes.perm.dto.userMenu.UiResourceDO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author zhj
 * @date 2022/1/18 13:41
 */
@Mapper
public interface UiResourceQueryMapper {
    @Results(value = {
            @Result(id = true, column = "objuid", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENT_UID", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "objuid", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "COMPONENT", property = "component", jdbcType = JdbcType.VARCHAR),
            @Result(column = "REDIRECT", property = "redirect", jdbcType = JdbcType.VARCHAR),
            @Result(column = "FORM_URL", property = "path", jdbcType = JdbcType.VARCHAR),

            @Result(column = "IMG_CLASS", property = "icon", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MENU_NAME", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "IS_ACTIVE", property = "show", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "IS_FRAME", property = "target", jdbcType = JdbcType.BOOLEAN),
//            @Result(column = "objuid", property = "hiddenHeaderContent", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PERMS", property = "permission", jdbcType = JdbcType.VARCHAR),
            @Result(column = "RESOURCE_TYPE", property = "resourceType", jdbcType = JdbcType.VARCHAR),
    })

    /**
     * 有顺序号ORDER_NO
     */
    @Select("SELECT\n" +
            "OBJUID,\n" +
            "MENU_NAME,\n" +
            "PARENT_UID,\n" +
            "FORM_URL,\n" +
            "REDIRECT,\n" +
            "CLIENT_TYPE,\n" +
            "IMG_CLASS,\n" +
            "ORDER_NO,\n" +
            "COMPONENT,\n" +
            "QUERY,\n" +
            "IS_FRAME,\n" +
            "IS_CACHE,\n" +
            "VISIBLE,\n" +
            "PERMS,\n" +
            "RESOURCE_TYPE,\n" +
            "IS_FILTER,\n" +
            "IS_ACTIVE\n" +
            "FROM PLAT_UI_RESOURCE \n" +
            "where client_type = 'sysm' and IS_ACTIVE=1 and resource_type in ('menu','page' ) \n" +
            "ORDER BY ORDER_NO ")
    List<UiResourceDO> selectList();

    @Results(id = "UiResourceDOMap",value = {
            @Result(id = true, column = "objuid", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENT_UID", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "objuid", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "COMPONENT", property = "component", jdbcType = JdbcType.VARCHAR),
            @Result(column = "REDIRECT", property = "redirect", jdbcType = JdbcType.VARCHAR),
            @Result(column = "FORM_URL", property = "path", jdbcType = JdbcType.VARCHAR),

            @Result(column = "IMG_CLASS", property = "icon", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MENU_NAME", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "IS_ACTIVE", property = "show", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "IS_FRAME", property = "target", jdbcType = JdbcType.BOOLEAN),
//            @Result(column = "objuid", property = "hiddenHeaderContent", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PERMS", property = "permission", jdbcType = JdbcType.VARCHAR),
            @Result(column = "RESOURCE_TYPE", property = "resourceType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "objuid", property = "children", many =
            @Many(select = "com.anolesoft.amljmes.perm.service.userMenu.mapper.UiResourceQueryMapper.selectListByParentId", fetchType = FetchType.DEFAULT)),
    })

    /**
     * 查询父主键为null
     */
    @Select("SELECT\n" +
            "OBJUID,\n" +
            "MENU_NAME,\n" +
            "PARENT_UID,\n" +
            "FORM_URL,\n" +
            "REDIRECT,\n" +
            "CLIENT_TYPE,\n" +
            "IMG_CLASS,\n" +
            "ORDER_NO,\n" +
            "COMPONENT,\n" +
            "QUERY,\n" +
            "IS_FRAME,\n" +
            "IS_CACHE,\n" +
            "VISIBLE,\n" +
            "PERMS,\n" +
            "RESOURCE_TYPE,\n" +
            "IS_FILTER,\n" +
            "IS_ACTIVE\n" +
            "FROM PLAT_UI_RESOURCE \n" +
            "where client_type = 'sysm' and IS_ACTIVE=1 and resource_type in ('menu','page' ) and PARENT_UID is null \n" +
            "ORDER BY ORDER_NO ")
    List<UiResourceDO> selectListTree();

//        @Select("select distinct g.OBJUID,\n" +
//                "       g.MENU_NAME ,\n" +
//                "       g.PARENT_UID,\n" +
//                "       g.FORM_URL,\n" +
//                "       g.REDIRECT,\n" +
//                "       g.CLIENT_TYPE,\n" +
//                "       g.IMG_CLASS,\n" +
//                "       g.ORDER_NO,\n" +
//                "       g.COMPONENT,\n" +
//                "       g.QUERY,\n" +
//                "       g.IS_FRAME,\n" +
//                "       g.IS_CACHE,\n" +
//                "       g.VISIBLE,\n" +
//                "       g.PERMS,\n" +
//                "       g.RESOURCE_TYPE,\n" +
//                "       g.IS_FILTER,\n" +
//                "       g.IS_ACTIVE " +
//                " from FAC_EMPLOYEE a\n" +
//                "\n" +
//                "  left join PERM_USER_AUTHORIZATION b\n" +
//                "    on a.objuid = b.user_uid\n" +
//                "\n" +
//                "  left join PERM_ROLE c\n" +
//                "    on b.role_uid = c.objuid\n" +
//                "\n" +
//                "  left join PERM_ROLEPERMISSION_REL d\n" +
//                "    on c.objuid = d.role_uid\n" +
//                "\n" +
//                "  left join PERM_PERMISSION e\n" +
//                "    on d.permission_uid = e.objuid\n" +
//                "\n" +
//                "  left join PERM_RESOURCE_AUTHORIZATION f\n" +
//                "    on e.objuid = f.permission_uid\n" +
//                "\n" +
//                "  left join PLAT_UI_RESOURCE g\n" +
//                "    on g.objuid = f.resource_uid\n" +
//                "\n" +
//                " where g.client_type = 'sysm'\n" +
//                "   and g.resource_type = 'menu'\n" +
//                "   and g.redirect is not null" +
//                "   and g.parent_uid is null" +
//                "   and a.objuid = #{user_uid,jdbcType=VARCHAR} \n" +
//                " ORDER BY g.ORDER_NO")
//    List<UiResourceDO> selectListTree(@Param("user_uid")String permissions);

    /**
     * 根据父主键进行查询 有哪些子页面
     * @param parentUid
     * @return
     */
    @Select("SELECT \n" +
            "OBJUID,\n" +
            "MENU_NAME,\n" +
            "PARENT_UID,\n" +
            "FORM_URL,\n" +
            "REDIRECT,\n" +
            "CLIENT_TYPE,\n" +
            "IMG_CLASS,\n" +
            "ORDER_NO,\n" +
            "COMPONENT,\n" +
            "QUERY,\n" +
            "IS_FRAME,\n" +
            "IS_CACHE,\n" +
            "VISIBLE,\n" +
            "PERMS,\n" +
            "RESOURCE_TYPE,\n" +
            "IS_FILTER,\n" +
            "IS_ACTIVE\n" +
            "FROM PLAT_UI_RESOURCE \n" +
            "where client_type = 'sysm' and IS_ACTIVE=1 and resource_type in ('menu','page' ) " +
            "and PARENT_UID= #{parentUid,jdbcType=VARCHAR} \n" +
            "ORDER BY ORDER_NO ")
    @ResultMap("UiResourceDOMap")
    List<UiResourceDO> selectListByParentId(@Param("parentUid") String parentUid);

            @Select("select f.resource_uid " +
                " from view_sys_perm_emp a\n" +
                "\n" +
                "  inner join PERM_USER_AUTHORIZATION b\n" +
                "    on a.objuid = b.user_uid\n" +
                "\n" +
                "  inner join PERM_ROLE c\n" +
                "    on b.role_uid = c.objuid\n" +
                "\n" +
                "  inner join PERM_ROLEPERMISSION_REL d\n" +
                "    on c.objuid = d.role_uid\n" +
                "\n" +
                "  inner join PERM_PERMISSION e\n" +
                "    on d.permission_uid = e.objuid\n" +
                "\n" +
                "  inner join PERM_RESOURCE_AUTHORIZATION f\n" +
                "    on e.objuid = f.permission_uid\n" +
                " where a.objuid = #{user_uid,jdbcType=VARCHAR} \n" )
    List<String> selectResourceByEmpUid(@Param("user_uid") String empUid);

}
